﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class ListArticles : System.Web.UI.Page
{
    protected PagedDataSource pds = new PagedDataSource();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindData();
        }
    }

    private void BindData()
    {
        CmsDataContext db = new CmsDataContext();
        var q = from aA in db.Article
                orderby aA.Published descending, aA.Title
                select new
                {
                    ID = aA.ID,
                    Title = aA.Title,
                    Subtitle = aA.Subtitle,
                    TopicID = aA.TopicID,
                    TopicName = aA.Topic.Name,
                    CreatorID = aA.CreatorID,
                    CreatorName = aA.Creator.Name,
                    VerifierID = aA.VerifyID,
                    VerifierName = aA.Verifier.Name,
                    Published = string.Format("{0:yyyy年MM月dd日<br/>HH:mm:ss}", aA.Published),
                    Verified = string.Format("{0:yyyy年MM月dd日<br/>HH:mm:ss}", aA.Verified),
                    Keywords = aA.Keywords,
                    Body = aA.Body
                };

        #region 检索条件
        string search = SearchConditionHidden.Value.Trim();
        if (search != "")
        {
            foreach (string s in search.Split(' '))
            {
                q = q.Where(aT => aT.Title.Contains(s) || 
                    aT.Subtitle.Contains(s)||
                    aT.TopicName.Contains(s)|| 
                    aT.CreatorName.Contains(s)|| 
                    aT.VerifierName.Contains(s)||
                    aT.Keywords.Contains(s)||
                    aT.Body.Contains(s));
            }
        }
        #endregion

        pds = new PagedDataSource();
        pds.AllowPaging = true;
        pds.PageSize = Toolkit.PageSize();
        pds.DataSource = q.ToList();


        #region 获取当前要显示的页
        int pageIndex = 0;
        pageIndex = Convert.ToInt32(CurrentPage.Value) - 1;
        if (pageIndex >= pds.PageCount)
        {
            pageIndex = pds.PageCount - 1;
        }
        if (pageIndex <= 0)
        {
            pageIndex = 0;
        }
        #endregion

        pds.CurrentPageIndex = pageIndex;

        ArticlesView.DataSource = pds;
        ArticlesView.DataBind();

        CurrentPage.Value = Convert.ToString(pageIndex + 1);
        SearchCondition.Text = SearchConditionHidden.Value.Trim();
    }

    protected void ChangePage(object sender, CommandEventArgs e)
    {
        string kind = Convert.ToString(e.CommandArgument);
        int p = Convert.ToInt32(CurrentPage.Value);

        if (kind == "F")
        {
            CurrentPage.Value = "1";
        }
        else if (kind == "P")
        {
            p--;
            CurrentPage.Value = Convert.ToString(p);
        }
        else if (kind == "N")
        {
            p++;
            CurrentPage.Value = Convert.ToString(p);
        }
        else if (kind == "L")
        {
            CurrentPage.Value = Convert.ToString(Int32.MaxValue);
        }
        else if (kind == "G")
        {
            TextBox gtp;
            foreach (Control c in  ArticlesView.Controls)
            {
                gtp = c.FindControl("GotoPage") as TextBox;
                if (gtp != null)
                {
                    try
                    {
                        CurrentPage.Value = Convert.ToString(Convert.ToInt32(gtp.Text.Trim()));
                    }
                    catch
                    {
                        //CurrentPage.Value = "1";
                    }

                    break;
                }
            }
        }

        BindData();
    }

    protected void DeleteArticle_Click(object sender, EventArgs e)
    {
        string tids = Request.Params["chkID"];

        if (tids != "")
        {
            string[] ids = tids.Split(',');
            CmsDataContext db = new CmsDataContext();

            #region 删除文章对应的附件
            var q1 = from aA in db.Annex
                     where ids.Contains(aA.ArticleID)
                     select aA;
            db.Annex.DeleteAllOnSubmit(q1);

            //todo:将来要删除对应的文件
            #endregion

            #region 删除文章对应的图片库
            var q2 = from aI in db.Illustrate
                     where ids.Contains(aI.ArticleID)
                     select aI;
            db.Illustrate.DeleteAllOnSubmit(q2);

            //todo:将来要删除对应的文件
            #endregion

            #region 删除该文章对应的静态文件
            //todo:将来要删除对用的文件
            #endregion

            #region 最后删除文章
            var q3 = from aA in db.Article
                     where ids.Contains(aA.ID)
                     select aA;
            db.Article.DeleteAllOnSubmit(q3);
            #endregion

            //提交数据库更新
            db.SubmitChanges();

            MessageBox.Message = "所选文章已删除。";
        }

        //重新绑定数据
        BindData();
    }

    protected void Search_Click(object sender, EventArgs e)
    {
        SearchConditionHidden.Value = SearchCondition.Text.Trim();
        BindData();
    }
}